package com.lumivi.devices;

import netP5.NetAddress;
import oscP5.OscMessage;
import oscP5.OscP5;
import processing.core.PApplet;
import processing.core.PFont;


public class XIMScreenServer extends PApplet { 
	
	PFont font; 

	OscP5 oscP5;
	NetAddress myRemoteLocation; 


	int bgColor;  
	float frequency = 4; 
	String msg = ""; 
	float angle = (float) 0.0; 

	public void setup() {
		size(screen.width, screen.height);
		frameRate(25);
		/* start oscP5, listening for incoming messages at port 12000 */
		oscP5 = new OscP5(this,12000); 
  
		//font = loadFont("Helvetica-Bold-200.vlw"); 
		
		font = createFont("Helvetica", 10);
		textAlign(CENTER); 

		this.bgColor = color(0); 
  
		frameRate(25); 
		//smooth(); 
  
	}


	public void draw() { 
  
  
		//color q = color((sin(frequency * PI * frameCount))); 
  
		
		angle += PI/(10*map(frequency, 0, 1, 10, 0)); 
		//println(255 * (sin(angle))); 
  
		//background(color(255 * (sin(angle)), 0, 0));  
		background(red(bgColor) * (sin(angle)), green(bgColor) * (sin(angle)), blue(bgColor) * (sin(angle))); 
  
		if (msg != "") { 
    
			textFont(font, 200); 
			fill(255); 
    
			text(msg, width/2, height/2);
    
		} 
	}


	/* incoming osc message are forwarded to the oscEvent method. */
	public void oscEvent(OscMessage theOscMessage) { 
		if(theOscMessage.checkAddrPattern("/videoColor") == true) { 

			/* print the address pattern and the typetag of the received OscMessage */
			print("### received an osc message."); 
			print(" addrpattern: "+theOscMessage.addrPattern()); 
			println(" typetag: "+theOscMessage.typetag()); 

			this.bgColor = theOscMessage.get(0).intValue(); 
			this.frequency = theOscMessage.get(1).floatValue(); 

		} 
  
  
		if(theOscMessage.checkAddrPattern("/message") == true) { 

			/* print the address pattern and the typetag of the received OscMessage */
			print("### received an osc message."); 
			print(" addrpattern: "+theOscMessage.addrPattern()); 
			println(" typetag: "+theOscMessage.typetag()); 

			this.msg = theOscMessage.get(0).stringValue(); 

		} 
	} 

} 

